package online.hxstart.code.java8.chapter01;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author huxing
 * @date 2019/6/19 星期三 19:37
 * @describe
 */
public class LambdaTest {


  public static void test1() {
    File[] hiddenFiles = new File("D:work.").listFiles(new FileFilter() {
      @Override
      public boolean accept(File file) {

        return file.isHidden();
      }
    });

  }


  public static void main(String[] args) {
    test1();
    List<Apple> inventory = Arrays.asList(new Apple("green", 150), new Apple("red", 100));
    filterApples(inventory, LambdaTest::isGreenApple);
  }


  public static List<Apple> filterHeavyApples(List<Apple> inventory) {
    List<Apple> result = new ArrayList<>();
    for (
        Apple apple : inventory) {
      if (apple.getWeight() > 150) {
        result.add(apple);
      }
    }
    return result;
  }

  static List<Apple> filterApples(List<Apple> inventory,
      Predicate<Apple> p) {
    List<Apple> result = new ArrayList<>();
    for (Apple apple : inventory) {
      if (p.test(apple)) {
        result.add(apple);
      }
    }
    return result;
  }

  public static boolean isGreenApple(Apple apple) {
    return "green".equals(apple.getColor());
  }

  public static boolean isHeavyApple(Apple apple) {

    Runnable re =
        () -> System.out.println(apple);
    new Thread(re).start();
    return apple.getWeight() > 150;
  }


}

@Data
@AllArgsConstructor
class Apple {

  private String color;
  private int weight;


}